/*
 * jobApplications.java
 *
 * Created on April 23, 2008, 2:11 PM
 */
import java.sql.SQLException;
import javax.swing.*;
import java.util.Calendar;


/**
 *
 * @author  Dustin
 */
public class jobApplications extends javax.swing.JFrame {
    dbAccess dbLink;
    recruiterMainPage parentDialog;
    int jobID = -1;
    /** Creates new form jobApplications */
    public jobApplications() {
        initComponents();
    }
    
    public void setDB(dbAccess link) {
        dbLink = link;
    }
    
    public void setParentDialog(recruiterMainPage parent) {
        parentDialog = parent;
    }
    
    public void setJobInfo(int id, String name) {
        jobID = id;
        jobIDlbl.setText(id + " : " + name);
        updateTable();
    }
    
    private void updateTable() {
        /*SELECT Applicant_ID, Name, Status, Test_Score
	FROM	((Applicant Natural JOIN Application) Natural JOIN Test)
	WHERE	Job_ID = &job_id;*/
        try {
            applicationTbl.setModel(dbLink.jobApplicationsStatus(jobID));
        } catch (SQLException se) {
            JOptionPane.showMessageDialog(null,
                                      new String[] {  // Display a 2-line message
                                          se.getClass().getName() + ": ",
                                          se.getMessage()
                                      });
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jobIDlbl = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        unfilledPoslbl = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        applicationTbl = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        advanceBtn = new javax.swing.JButton();
        declineBtn = new javax.swing.JButton();
        closeBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Application status update for job ID: ");

        jobIDlbl.setText("[JobID] : [Job Name]");

        jLabel2.setText("Number of unfilled positions: ");

        unfilledPoslbl.setText("[Unfilled Positions]");

        applicationTbl.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Selected", "Applicant ID", "Application ID", "Name", "Status", "Test Score"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.Object.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        applicationTbl.getTableHeader().setReorderingAllowed(false);
        applicationTbl.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                applicationTblMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(applicationTbl);

        jLabel3.setText("*Click value in Test Score cell to edit score");

        advanceBtn.setText("Advance");
        advanceBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                advanceBtnActionPerformed(evt);
            }
        });

        declineBtn.setText("Decline");
        declineBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                declineBtnActionPerformed(evt);
            }
        });

        closeBtn.setText("Close Window");
        closeBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jobIDlbl)
                        .addGap(40, 40, 40)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(unfilledPoslbl))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(advanceBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(declineBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 121, Short.MAX_VALUE)
                        .addComponent(closeBtn)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jobIDlbl)
                    .addComponent(jLabel2)
                    .addComponent(unfilledPoslbl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(advanceBtn)
                        .addComponent(declineBtn)
                        .addComponent(closeBtn)))
                .addGap(11, 11, 11))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeBtnActionPerformed
        // TODO add your handling code here:
        parentDialog.setEnabled(true);
        parentDialog.setVisible(true);
        dispose();
    }//GEN-LAST:event_closeBtnActionPerformed

    private void applicationTblMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_applicationTblMouseClicked
        // TODO add your handling code here:
        int col = applicationTbl.getSelectedColumn();
        int row = applicationTbl.getSelectedRow();
        if(col == 3) {
            displayApplicantProfile(Integer.parseInt(applicationTbl.getModel().getValueAt(row, 1).toString()));
        }
    }//GEN-LAST:event_applicationTblMouseClicked

    private void declineBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_declineBtnActionPerformed
        // TODO add your handling code here:
        int row = applicationTbl.getSelectedRow();
        int appID = Integer.parseInt(applicationTbl.getModel().getValueAt(row, 2).toString());
        try {
            dbLink.declineApplication(appID);
        } catch (SQLException se) {
            JOptionPane.showMessageDialog(null,
                                      new String[] {  // Display a 2-line message
                                          se.getClass().getName() + ": ",
                                          se.getMessage()
                                      });
        }
        updateTable();
    }//GEN-LAST:event_declineBtnActionPerformed

    private void advanceBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_advanceBtnActionPerformed
        // TODO add your handling code here: //VINUTHA
        int row = applicationTbl.getSelectedRow();
        int appId = Integer.parseInt(applicationTbl.getModel().getValueAt(row, 2).toString());
        String status = applicationTbl.getModel().getValueAt(row, 4).toString();
        try {
                dbLink.advanceApplication(appId, status);
        } catch (SQLException se) {
             JOptionPane.showMessageDialog(null,
                                      new String[] {  // Display a 2-line message
                                          se.getClass().getName() + ": ",
                                          se.getMessage()
                                      });
        }
        updateTable();
    }//GEN-LAST:event_advanceBtnActionPerformed
    
    private void displayApplicantProfile(int ID) {
        String[] applicantData = null;
        String[] msgData = new String[9];
        try {
            applicantData = dbLink.getApplicantProfileWithID(ID);
        } catch (SQLException se) {
            JOptionPane.showMessageDialog(null,
                                      new String[] {  // Display a 2-line message
                                          se.getClass().getName() + ": ",
                                          se.getMessage()
                                      });
        }
        if(applicantData != null) {
            Calendar calendar = Calendar.getInstance();
            int currentYear = calendar.get(Calendar.YEAR);
            int age = currentYear - Integer.parseInt(applicantData[7]);
            
            msgData[0] = "Applicant Detail";
            msgData[1] = "Name: " + applicantData[0];
            msgData[2] = "Email: " + applicantData[1];
            msgData[3] = "Phone: " + applicantData[2];
            msgData[4] = "Highest Degree: " + applicantData[3];
            msgData[5] = "Age: " + age;
            msgData[6] = "Years of Experience: " + applicantData[4];
            msgData[7] = "Citizenship: " + applicantData[5];
            msgData[8] = "Description: " + applicantData[6];
            
            JOptionPane.showMessageDialog(null, msgData);
        }
    }
        
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new jobApplications().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton advanceBtn;
    private javax.swing.JTable applicationTbl;
    private javax.swing.JButton closeBtn;
    private javax.swing.JButton declineBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jobIDlbl;
    private javax.swing.JLabel unfilledPoslbl;
    // End of variables declaration//GEN-END:variables
    
}
